//
//  MLWMenuViewController.m
//  KITCHEN_STORIES
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWMenuViewController.h"
#import "MLWMenuCollectionViewCell.h"
#import "MLWMenuModel.h"
#import "MLWSmallModel.h"
#import "UIImageView+WebCache.h"
#import "MLWDetailViewController.h"
#import "MLWSecondViewController.h"
#import "EATheme.h"
@interface MLWMenuViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
/** 定义collectionView属性 */
@property(nonatomic, retain)UICollectionView *collertionView;

/** 返回section的个数 */
@property (nonatomic, assign) NSInteger countNum;

/** 数据源数组 */
@property (nonatomic, retain) NSMutableArray *dataSource;

/** 设置动态翻页属性 */
@property (nonatomic, retain) CATransition *transition;
@end

@implementation MLWMenuViewController

- (void)dealloc
{
    [self.collertionView release];

   
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //请求数据:
    [self getByKey:@"MyMenu"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"MyMenu";
    
    //初始化数据源数组:
    self.dataSource = [NSMutableArray array];
    
    //初始化默认瀑布流对象:
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    //设置item的尺寸:
    flowLayOut.itemSize = CGSizeMake(80, 80);
    //设置最小列间距:
    flowLayOut.minimumLineSpacing = 5;
    //设置最小行间距:
    flowLayOut.minimumInteritemSpacing = 5;
    //设置内边距:
    flowLayOut.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    
    //初始化collectionView
    self.collertionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayOut];
    self.collertionView.backgroundColor = [UIColor whiteColor];
    
    self.collertionView.delegate = self;
    self.collertionView.dataSource = self;
    
    //设置头部引用尺寸:
    flowLayOut.headerReferenceSize = CGSizeMake(0, 30);
    flowLayOut.footerReferenceSize = CGSizeMake(0, 10);
    
    [self.view addSubview:self.collertionView];
    [self.collertionView release];
    
    //注册cell
    [self.collertionView registerClass:[MLWMenuCollectionViewCell class] forCellWithReuseIdentifier:@"MenuCell"];
    
    //注册section的头视图:
    [self.collertionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //注册section的尾部视图:
    [self.collertionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    //夜间模式:
    [self.collertionView ea_setThemeContents:^(UIView *currentView, NSString *currentThemeIdentifier) {
        
        if ([currentThemeIdentifier isEqualToString:EAThemeBlack]) {
            self.collertionView.backgroundColor = [UIColor colorWithRed:0.08 green:0.08 blue:0.08 alpha:1.00];
            
        }
        
        else
        {
            self.collertionView.backgroundColor = [UIColor whiteColor];
            
        }
        
    }];

    
    
    //1.初始化
    self.transition = [CATransition animation];
    //2.设置动画时长,设置代理人
    self.transition.duration = 1.5f;
    self.transition.delegate = self;       //id  不用签协议
    //3.设置切换速度效果
    self.transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //4.动画切换风格
    self.transition.type = kCATransitionReveal;       //淡入淡出
    //5.动画切换方向
    self.transition.subtype = kCATransitionFromRight;//右侧
}

#pragma mark ----- 请求解析数据:
- (void)getByKey:(NSString *)keyString
{
    [MLWNetTool GetNetWithUrl:@"http://121.41.103.24/public/getRecipeHomeData.shtml" body:nil header:nil response:MLWJSON success:^(id result) {
        [LTArchiver archiverObject:result ByKey:@"menuOfMy" WithPath:@"menuOfMy.plist"];
        NSDictionary *dicME = (NSDictionary *)result;
        NSMutableArray *arrayList = [dicME objectForKey:@"list"];
        
        //[LTArchiver archiverObject:arrayList ByKey:@"Menu" WithPath:@"Menu.plist"];

        
        for (NSDictionary *dicList in arrayList) {
            
            MLWMenuModel *model = [[MLWMenuModel alloc] initWithDic:dicList];
         
            [self.dataSource addObject:model];
            [model release];
        }
        
        [self.collertionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSDictionary *result = [LTArchiver unarchiverObjectByKey:@"menuOfMy" WithPath:@"menuOfMy.plist"];
        
        NSMutableArray *arrayList = [result objectForKey:@"list"];
        
       // [LTArchiver archiverObject:arrayList ByKey:@"Menu" WithPath:@"Menu.plist"];
        
        
        for (NSDictionary *dicList in arrayList) {
            
            MLWMenuModel *model = [[MLWMenuModel alloc] initWithDic:dicList];
            
            [self.dataSource addObject:model];
            [model release];
        }
        
        [self.collertionView reloadData];

        
    }];
}


#pragma mark ----- 返回每个分区的头视图/尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //MLWMenuModel *model = [self.dataSource objectAtIndex:indexPath.section];
    
    self.countNum = [[self.dataSource objectAtIndex:indexPath.section] smallModelArray].count;
    //头视图:
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
                    [headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, ScreenWidth / 3, 20)];
            headerLabel.text = [[self.dataSource objectAtIndex:indexPath.section] name];
            [headerView addSubview:headerLabel];
        //夜间模式:
        [headerView ea_setThemeContents:^(UIView *currentView, NSString *currentThemeIdentifier) {
            
            if ([currentThemeIdentifier isEqualToString:EAThemeBlack]) {
                headerLabel.textColor = [UIColor yellowColor];
                
            }
            
            else
            {
                self.collertionView.backgroundColor = [UIColor whiteColor];
                
            }
            
        }];

        
        return headerView;
    }
    //尾部视图:
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];

        footerView.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:0.99 alpha:1.00];
        return footerView;
    }
    return nil;
    
}
#pragma mark ----- section分区个数:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return self.dataSource.count;
    
}


#warning mark ----- 必须实现的代理方法:
#pragma mark ----- 每个section有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    MLWMenuModel *model = [self.dataSource objectAtIndex:section];
    
    
    return model.list.count;
    
    
}

//重用:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MLWMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuCell" forIndexPath:indexPath];
    
     MLWMenuModel *model = [self.dataSource objectAtIndex:indexPath.section];
    
    //给每个Item赋图片:
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:IMAGE_URL([[model.smallModelArray objectAtIndex:indexPath.item] imageid])]placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    
    //给每个Item赋标题:
    cell.topLabel.text = [[model.smallModelArray objectAtIndex:indexPath.item] name];
    

    return cell;
    
}

#pragma mark ----- item 的点击方法:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MLWSecondViewController *svc = [[MLWSecondViewController alloc] init];
    
    // 6.进行跳转
    [self.navigationController.view.layer addAnimation:self.transition forKey:nil];
    [self.navigationController pushViewController:svc animated:NO];
    
    //[self.navigationController pushViewController:svc animated:YES];
   
    MLWMenuModel *model = [self.dataSource objectAtIndex:indexPath.section];
    
    svc.titleStr = [[model.smallModelArray objectAtIndex:indexPath.row] name];
    svc.urlId = [[model.smallModelArray objectAtIndex:indexPath.row] id];
    [svc release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
